package com.ruoyi.project.system.newfunction.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.newfunction.domain.Depts;
import com.ruoyi.project.system.newfunction.service.DeptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/system/depts")
@RestController
public class DeptsController extends BaseController {
    @Autowired
    private DeptsService deptsService;

    /**
     * 基于分页查询科室
     * @param depts
     * @return
     */
    @RequestMapping("/list")
    @PreAuthorize("@ss.hasPermi('system:depts:list')")
    public TableDataInfo findAllByPage(Depts depts){
        startPage();
        List<Depts> allByPage = deptsService.findAllByPage(depts);
        return getDataTable(allByPage);
    }

    /**
     * 新增科室
     * @param depts
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("@ss.hasPermi('system:depts:add')")
    public AjaxResult saveDepts(@RequestBody Depts depts){
        int i = deptsService.saveDepts(depts);
        return toAjax(i);
    }

    /**
     *  根据科室编号查询科室信息
     * @param deptsId
     * @return
     */
    @GetMapping("/{deptsId}")
    @PreAuthorize("@ss.hasPermi('system:depts:list')")
    public AjaxResult findAllByPage(@PathVariable Integer deptsId){
        return AjaxResult.success(deptsService.findDpetsById(deptsId));
    }

    /**
     * 修改科室信息
     * @param depts
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('system:depts:update')")
    public AjaxResult updateDepts(@RequestBody Depts depts){
        return toAjax(deptsService.updateDepts(depts));
    }

    /**
     * 删除角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @DeleteMapping("/{deptsIds}")
    public AjaxResult remove(@PathVariable Long[] deptsIds) {
        return toAjax(deptsService.removeDepts(deptsIds));
    }


}
